﻿using System;
using System.Linq;
using System.Custom.Window;
using System.Collections.ObjectModel;

namespace CoreTypes
{
   public class AccountSet : KeyedCollection<string, Account>
   {
      public AccountSet()
         : base()
      {
      }
      protected override string GetKeyForItem(Account item)
      {
         return item.Identifier;
      }
      public Account GetItemById(string id)
      {
         if (id == null) return null;

         Account account = null;
         if (this.Dictionary != null) this.Dictionary.TryGetValue(id, out account);
         return account;
      }
   }

   public class ContactList : KeyedCollection<string, Contact>
   {
      public ContactList()
         : base()
      {

      }

      public Contact GetItemById(string id)
      {
         if (id == null) return null;

         Contact contact = null;
         if (this.Dictionary != null) this.Dictionary.TryGetValue(id, out contact);
         return contact;
      }

      protected override string GetKeyForItem(Contact item)
      {
         return item.Identifier;
      }
   }

   public class ColorThemes : KeyedCollection<string, ColorTheme>
   {
      public ColorThemes()
         : base()
      {

      }

      public ColorTheme GetItemById(string id)
      {
         if (id == null) return null;

         ColorTheme theme = null;
         if (this.Dictionary != null) this.Dictionary.TryGetValue(id, out theme);
         return theme;
      }

      protected override string GetKeyForItem(ColorTheme item)
      {
         return item.Name;
      }
   }

   public class MediaSessionSet : KeyedCollection<string, IMediaSession>
   {
      bool FExclusiveTypes;

      public MediaSessionSet(bool exclusiveTypes)
         : base()
      {
         FExclusiveTypes = exclusiveTypes;
      }

      bool ExclusiveTypes
      {
         get
         {
            return FExclusiveTypes;
         }
      }

      protected override string GetKeyForItem(IMediaSession item)
      {
         return item.Gsid;
      }
      public bool AddItem(string key, IMediaSession newItem)
      {
         if (GetItemById(newItem.Gsid) == null)
         {
            if (!FExclusiveTypes || GetItemByFullType(newItem.GetType().Name, newItem.Subtype) == null)
            {
               Add(newItem);
               return true;
            }
            else
            {
               //throw new Exception("Session of this type is already exist in the set.");
            }
         }
         return false;
      }
      public IMediaSession GetItemById(string gsid)
      {
         if (gsid == null) return null;

         IMediaSession item = null;
         if (this.Dictionary != null) this.Dictionary.TryGetValue(gsid, out item);
         return item;
      }
      public IMediaSession GetItemByFullType(string typeName, SessionSubtype subtype)
      {
         return this.FirstOrDefault(session => session.GetType().Name == typeName && session.Subtype == subtype);
      }
      public bool ChangeSessionGsid(IMediaSession session, string newGsid)
      {
         session.Gsid = newGsid;

         ChangeItemKey(session, newGsid);

         return true;
      }
   }

   public class NetworkOperationSet : KeyedCollection<string, NetworkOperation>
   {
      public NetworkOperationSet()
         : base()
      {
      }

      public bool ContainsName(string name)
      {
         return this.Count(o => o.Name.ToLower() == name.ToLower()) != 0;
      }
      public bool AddItem(NetworkOperation item)
      {
         if (Contains(item.Id.ToString())) return false;

         Add(item);

         return true;
      }
      public NetworkOperation GetItemById(string id)
      {
         if (id == null) return null;

         NetworkOperation nwo = null;
         if (this.Dictionary != null) this.Dictionary.TryGetValue(id, out nwo);
         return nwo;
      }
      public NetworkOperation GetItemByName(string name)
      {
         if (name == null) return null;

         return this.FirstOrDefault(o => o.Name.ToLower() == name.ToLower());
      }
      protected override string GetKeyForItem(NetworkOperation item)
      {
         return item.Id.ToString();
      }
   }
}